import axios from 'axios';
import { Toast } from 'antd-mobile';

let host;
if (process.env.NODE_ENV === 'development') {
	host = 'http://139.159.151.89:8080';
// 	// host = 'http://192.168.112.128:8000';
} else if (process.env.NODE_ENV === 'production') {
	host = '/api';
// let host = 'http://49.233.129.163:8080';
}

const baseUri = host + '/coupon/'

// 请求拦截
axios.interceptors.request.use( function(config) {
	config.headers = {
		"Content-Type": "application/json"
	}
	return config
}, function(error) {
	return Toast.fail('服务端错误', 1);
})

// 响应拦截
axios.interceptors.response.use( function(response) {
	const { error, msg } = response.data;
	if (!response) {
		return Toast.fail('请求失败', 1);
	}

	if (process.browser && parseInt(error)) {
		Toast.fail(msg, 1);
	}

	return response;
}, function(error) {
	return Toast.fail('服务端错误', 1);
})

export const getRequest = async function(url, params) {
	const result = await axios.get(baseUri + url, {params});
	if (result.data) {
		return result.data
	}
}

export const postRequest =  async function(url, params) {
	const result = await axios.post(baseUri + url, params);
	if (result.data) {
		return result.data
	}
}